package top.qrcodepay.plugin.map;

import java.awt.image.BufferedImage;

import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;

public class QrcodeRenderer extends MapRenderer {
	
	private BufferedImage image;
	
	@Override
	public void render(MapView view, MapCanvas canvas, Player player) {
		if (image == null) return;
		canvas.drawImage(0, 0, image);
		image = null;
	}

	public static void installRenderer(BufferedImage image, MapView map) {
		installRenderer(map).setImage(image);
	}
    
	public void setImage(BufferedImage image) {
		this.image = image;
	}

	public static QrcodeRenderer installRenderer(MapView map) {
		QrcodeRenderer renderer = new QrcodeRenderer();
		removeRenderers(map);
		map.addRenderer(renderer);
		return renderer;
    }
    
	public static void removeRenderers(MapView map) {
    	for(MapRenderer renderer : map.getRenderers()) {
    		map.removeRenderer(renderer);
    	}
	}

}
